<template>
  <div>
    <el-table
      :data="cartItemList"
      :stripe="true"
      style="width: 100%">
      <el-table-column
        label="书名">
        <template slot-scope="scope">
          {{scope.row.book.bookName}}
        </template>
      </el-table-column>
      <el-table-column
        label="原价">
        <template slot-scope="scope">
          {{scope.row.book.bookPrice}}
        </template>
      </el-table-column>
      <el-table-column
        label="押金">
        <template slot-scope="scope">
          {{scope.row.book.dangprice}}
        </template>
      </el-table-column>
      <el-table-column
        label="租金(/天)">
        <template slot-scope="scope">
          {{scope.row.book.borrowPrice}}
        </template>
      </el-table-column>
      <el-table-column
        prop="count"
        label="天数">
      </el-table-column>

      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="deleteCart(scope.row.book.bookId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row :gutter="10" v-if="cartItemList.length>0">
     <el-col :span="18">总金额：{{cart.totalMoney}}元</el-col>
     <el-col :span="6"><el-button @click="addOrder">生成订单</el-button></el-col>
    </el-row>
  </div>
</template>

<script>
    export default {
        name: "cart",
      data(){
          return {
            cart:{},
            cartItemList:[]
          }
      },
      created(){
        this.selectCart();
      },
      methods:{
          selectCart(){
            //发出ajax请求请求后端代码
            this.axios.get("/cart/myCart").then((resp)=>{
              this.cart=resp.data;
              if((Object.keys(this.cart.buyMap)).length>0){
                for(let i in this.cart.buyMap){
                  this.cartItemList.push(this.cart.buyMap[i]);
                }
              }


            });
          },
          addOrder(){
            this.axios.post("/order/genateOrder").then((resp)=>{
              this.$message.success("订单添加成功");
              this.$router.push("/cart/MyOrder");
            })
          },
        deleteCart(id){
          this.axios.delete("/cart/delete/"+id).then((resp)=>{
            this.$message.success("删除成功");
            this.cartItemList.splice(0,this.cartItemList.length);
            this.selectCart();
          })
        }
      }
    }
</script>

<style scoped>

</style>
